package com.ta.service.impl;

import com.ta.mapper.AddressMapper;
import com.ta.pojo.Address;
import com.ta.pojo.District;
import com.ta.service.AddressService;
import com.ta.service.ex.AddressException;
import com.ta.service.ex.AddressLimitException;
import com.ta.service.ex.InsertException;
import com.ta.service.ex.UpdateException;
import com.ta.util.ThrowServiceException;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl extends ThrowServiceException implements AddressService {

    @Autowired
    private AddressMapper addressMapper;
    @Value("${address.limit}")
    private Integer limit;
    /**
     * 实现增加地址业务接口
     * @param address
     */
    @Override
    public void insert(Address address, Integer uid, String userName) {
        Integer count = addressMapper.countAddress(uid);
        if(count>limit){
            throw new AddressLimitException("地址数量超出限制！");
        }
        /*第一次插入地址*/
        if (count == 0) {
            address.setIsDefault(1);
        }
        String province = addressMapper.findByCode(address.getProvinceCode());
        String city = addressMapper.findByCode(address.getCityCode());
        String area = addressMapper.findByCode(address.getAreaCode());
        address.setProvinceName(province);
        address.setCityName(city);
        address.setAreaName(area);

        address.setUid(uid);
        address.setCreatedUser(userName);
        address.setCreatedTime(new Date());
        Integer row = addressMapper.insert(address);
        if (row != 1) {
            throw new InsertException("插入地址出现未知异常");
        }
    }

    /**
     * 实现地址信息业务接口
     * @param uid
     */
    @Override
    public List<Address> findByUid(Integer uid) {
        List<Address> addresses = addressMapper.findByUid(uid);
        return addresses;
    }

    /**
     * 实现默认地址业务接口
     * @param aid
     * @param uid
     * @param userName
     */
    @Override
    public void ModifyDefaultAid(Integer aid, Integer uid, String userName) {
        throwAddressNoFound(aid);
        throwAddressIllegalAccess(aid, uid);

        Integer noDefault = addressMapper.modifyUid(uid, userName, new Date());
        if(noDefault <= 0){
            throw new UpdateException("设置默认地址是出现未知异常");
        }

        Integer row = addressMapper.modifyDefaultAid(aid, userName, new Date());
        if(row != 1){
            throw new UpdateException("设置默认地址是出现未知异常");
        }
    }

    /**
     * 实现删除地址业务接口
     * @param aid
     */
    @Override
    public void delete(Integer aid, Integer uid) {
        throwAddressNoFound(aid);
        throwAddressIllegalAccess(aid, uid);

        Integer row = addressMapper.delete(aid);
        if(row != 1){
            throw new AddressException("删除地址出现未知异常");
        }
    }

    /**
     * 实现省份管辖城市业务接口
     * @param parent
     * @return
     */
    @Override
    public List<District> findByParent(String parent) {
        List<District> district = addressMapper.findByParent(parent);
        return district;
    }

    /**
     * 实现修改地址业务接口
     * @param address
     * @param aid
     * @param userName
     */
    @Override
    public void modify(Address address, Integer aid, String userName, Integer uid) {
        throwAddressNoFound(aid);
        throwAddressIllegalAccess(aid, uid);

        String province = addressMapper.findByCode(address.getProvinceCode());
        String city = addressMapper.findByCode(address.getCityCode());
        String area = addressMapper.findByCode(address.getAreaCode());
        address.setProvinceName(province);
        address.setCityName(city);
        address.setAreaName(area);

        address.setAid(aid);
        address.setModifiedUser(userName);
        address.setModifiedTime(new Date());
        Integer row = addressMapper.modify(address);
        if(row != 1){
            throw new AddressException("修改地址出现未知异常");
        }
    }

    /**
     * 实现当前地址信息业务接口
     * @param aid
     * @return
     */
    @Override
    public Address findByAid(Integer aid) {
        return addressMapper.findByAid(aid);
    }

}
